
package nativesql.entity;

import java.util.List;

import model.bean.District;

import org.hibernate.SQLQuery;

import com.e885.hibernate.HibernateSessionFactory;
import com.e885.log.E885Logger;

/**
 * 使用 Entity 轉換為物件, 將整個 table 的 coloumn 一定要整個列出來, 少一個會有錯誤, 會出現 "不正確的資料欄名稱" 錯誤訊息
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2001-2008
 */

public class EntityNativeSQLAlias2
{
	/**
	 * 查詢台北縣的鄉鎮市
	 */
	private String countyId = "F";

	public static void main( String[] args )
	{
		new EntityNativeSQLAlias2();
	}

	@SuppressWarnings( "unchecked" )
	public EntityNativeSQLAlias2()
	{
		String sql =
					 "select ID as {district.id}, NAME as {district.name}, COUNTY_ID as {district.county}  from DISTRICT district where COUNTY_ID=:countyId";

		SQLQuery query = HibernateSessionFactory.getSession().createSQLQuery( sql );
		query.addEntity( "district", District.class );

		query.setParameter( "countyId", countyId );

		List< District > list = query.list();

		for( int i = 0; i < list.size(); i++ )
		{
			District distrcit = list.get( i );

			E885Logger.testLog.debug( "=======================================================" );
			E885Logger.testLog.debug( "id:" + distrcit.getId() );
			E885Logger.testLog.debug( "name:" + distrcit.getName() );
			E885Logger.testLog.debug( "county name:" + distrcit.getCounty().getName() );
		}
	}

}
